package torres.item.interactive;

import java.util.ArrayList;
import java.util.List;

import torres.action.Action;
import torres.entity.Entity;
import torres.fileIO.BasicContainer;
import torres.fileIO.ClassContainer;
import torres.item.Item;


/**
 * @author Torres
 * Represents an item that is placed on the map and is consumed
 * only if certain conditions are met.
 */

public class InteractiveItem extends Item
{
	private List<Interaction> intList;
	private List<Action> actions;
	
	/**
	 * Creates a new InteractiveItem with the specified name and no Interactions.
	 * @param name The name of the Interactive item.
	 */
	public InteractiveItem(String name,int id) {
		super(name, id);
		//this.setName(name);
		
		//this.imageName = "sword.png";
		this.intList = new ArrayList<Interaction>();
	}
	
	/**
	 * Creates a new InteractiveItem with the specified name and list of Interactions.
	 * @param name The name of the Interactive item.
	 * @param intList The list of Interactions to be applied on use.
	 */
	public InteractiveItem(String name, int id, List<Interaction> intList) {
		this(name, id);
		this.setInteractions(intList);
	}
	
	/**
	 * Creates a new InteractiveItem with specified attributes.
	 * @param name Name of the Interactive item
	 * @param id ID of the Interactive item
	 * @param intList List of Interactions to be applied on use.
	 * @param actions List of Actions to be applied on use. 
	 */
	public InteractiveItem(String name, int id, List<Interaction> intList, List<Action> actions) {
		this(name, id, intList);
		this.setActions(actions);
	}
	
	/**
	 * Creates a new InteractiveItem which is a copy of the specified InteractiveItem.
	 * @param item The InteractiveItem to be copied.
	 */
	public InteractiveItem(InteractiveItem item) {
		this(item.getName(), item.getID(), item.getInteractions(), item.getActions());
	}
	
	/**
	 * Triggers the effect of an Entity walking on this item.
	 * @param e The Entity that is walking on this item
	 */
	public void onTouch(Entity e) {
		if(this.getActions() != null) 
			for(Action a : this.getActions()) {
				a.setUp(e);
				Thread t = new Thread(a);
				t.setName("IntItem" + t.getId());
				t.start();
			}
		if(this.getInteractions() != null)
			for(Interaction i : this.getInteractions()) 
				i.use(e);
	}
	
	
	/**
	 * Returns the list of Interactions that this item performs on touch.
	 * @return The list of Interactions that this item performs on touch.
	 */
	protected List<Interaction> getInteractions() {
		return this.intList;
	}
	
	/**
	 * Sets the list of Interactions that this item performs on touch.
	 * @param ints The list of Interactions that this item performs on touch.
	 */
	private void setInteractions(List<Interaction> ints) {
		this.intList = ints;
	}
	
	/**
	 * Get the list of Actions that occur on use.
	 * @return The list of Actions that occur on use.
	 */
	protected List<Action> getActions() {
		return this.actions;
	}
	
	/**
	 * Set the actions of this item to the specified list.
	 * @param actions The new list of Actions.
	 */
	private void setActions(List<Action> actions) {
		this.actions = actions;
	}
	
	/**
	 * Adds the specified Interaction to this InteractiveItem.
	 * @param interaction The Interaction to be added to this InteractiveItem.
	 */
	public void addInteraction(Interaction interaction) {
		
		intList.add(interaction);
	}

	/**
	 * Adds all of the Interactions in the List to this InteractiveItem.
	 * @param intList The list of Interactions to be added to this InteractiveItem.
	 */
	public void appendInteractionList(List<Interaction> intList)
	{
		this.intList.addAll(intList);
	}

	/* (non-Javadoc)
	 * @see torres.item.Item#save()
	 */
	@Override
	public ClassContainer save() {
		ClassContainer curClass = super.save();
		curClass.addProperty(new BasicContainer("type", "InteractiveItem"));
		return curClass;
	}
/*
	/* (non-Javadoc)
	 * @see torres.item.Item#load(torres.fileIO.ClassContainer)
	 */
	@Override
	public void load(ClassContainer classContainer) {
		// TODO Auto-generated method stub
		
	}
}
